;;; init-ui.el --- UI settings -*- lexical-binding: t -*-
;;; Commentary:

;;; Code:

;; nerd icon 支持
(use-package nerd-icons
  :ensure t
  :custom
  (nerd-icons-font-family "Symbols Nerd Font"))

;; 为minibuffer中的选项添加图标
(use-package nerd-icons-completion
  :ensure t
  :hook ((elpaca-after-init . nerd-icons-completion-mode)
         (marginalia-mode . #'nerd-icons-completion-marginalia-setup)))

;; From: github.com/seagle0128/.emacs.d/blob/c9bd6f1bb72486580f55879cdfd4fdcc852a49a6/lisp/init-funcs.el#L467
;; 定义主题加载后的钩子函数
(defvar after-load-theme-hook nil
  "Normal hook run after loading a theme.")

(defun run-after-load-theme-hook (&rest _args)
  "Run `after-load-theme-hook`."
  (run-hooks 'after-load-theme-hook))
(advice-add 'load-theme :after #'run-after-load-theme-hook)

(use-package ef-themes :defer t :ensure t)

;; 自动切换主题
(use-package theme-changer
  :ensure t
  :config
  (change-theme 'ef-frost 'ef-maris-light))

(use-package fontaine
  :ensure t
  :config
  (setq fontaine-presets
        '((medium
           :default-family "SF Mono"
           :default-weight medium
           :variable-pitch-family "Bookerly"
           :mode-line-active-family "Sarasa Mono SC"
           :mode-line-inactive-family "Sarasa Mono SC"
           :tab-bar-family "Sarasa Mono SC")
          (t
           :line-spacing 1)))
  (fontaine-set-preset 'medium))

(defun font-installed-p (font-name)
  "Check if font with FONT-NAME is available."
  (find-font (font-spec :name font-name)))

;; From: github.com/seagle0128/.emacs.d/blob/master/custom-example.el
(defun +ui--setup-fonts ()
  "Setup fonts."
  (when (display-graphic-p)
    ;; Specify font for all unicode characters
    (cl-loop for font in '("Segoe UI Symbol" "Symbola" "Symbol")
             when (font-installed-p font)
             return (if (< emacs-major-version 27)
                        (set-fontset-font "fontset-default" 'unicode font nil 'prepend)
                      (set-fontset-font t 'symbol (font-spec :family font) nil 'prepend)))

    ;; Emoji
    (cl-loop for font in '("Segoe UI Emoji" "Apple Color Emoji" "Noto Color Emoji")
             when (font-installed-p font)
             return (cond
                     ((< emacs-major-version 27)
                      (set-fontset-font "fontset-default" 'unicode font nil 'prepend))
                     ((< emacs-major-version 28)
                      (set-fontset-font t 'symbol (font-spec :family font) nil 'prepend))
                     (t
                      (set-fontset-font t 'emoji (font-spec :family font) nil 'prepend))))

    ;; Specify font for Chinese characters
    (cl-loop for font in '("FZYouSongJ GBK 509R" "Sarasa Mono SC" "Microsoft Yahei UI" "PingFang SC" "Noto Sans SC")
             when (font-installed-p font)
             return (progn
                      (dolist (charset '(kana han cjk-misc bopomofo))
                        (set-fontset-font t charset (font-spec :family font)))))))

(add-hook 'fontaine-set-preset-hook #'+ui--setup-fonts)

(custom-set-faces
 '(org-table ((t (:inherit unspecified :family "Sarasa Mono SC" :height 0.98)))); 只要缩小一点就能让表格对齐，WTF???
 '(dashboard-items-face ((t (:inherit unspecified :family "Sarasa Mono SC")))))

;; help 页面美化
(use-package helpful :defer 2 :ensure t)

(use-package visual-fill-column
  :ensure (:repo "https://codeberg.org/joostkremers/visual-fill-column")
  :hook ((org-mode markdown-mode) . (lambda ()
                                      (setq-local visual-fill-column-width 120)
                                      (when (derived-mode-p 'org-mode)
                                        (setq-local visual-fill-column-center-text t))
                                      (visual-fill-column-mode 1))))

;; 启动界面
(use-package dashboard
  :ensure t
  :init
  (setq dashboard-projects-backend 'projectile ; 'projectile or 'project-el
        dashboard-path-style 'truncate-middle  ; 路径截断风格
        dashboard-path-max-length 60	       ; 路径最大长度
        dashboard-center-content t	       ; 内容居中
        dashboard-footer-icon (propertize ">" 'face 'dashboard-footer-icon-face))
  ;; 随机 banner
  (setq dashboard-startup-banner
        (let* ((directory (concat no-littering-etc-directory "banners/"))
               (files (directory-files directory t "^[1-9]?[0-9]\\.txt$"))
               (filtered-files (seq-filter (lambda (file)
                                             (let ((file-number (string-to-number (file-name-base file))))
                                               (and (>= file-number 1) (<= file-number 99))))
                                           files)))
          filtered-files))

  (setq dashboard-banner-logo-title "Welcome to Emacs Dashboard") ;个性签名

  (setq dashboard-heading-shorcut-format " [%s]" ; 快捷键样式
        ;; Customize item shortcuts
        dashboard-item-shortcuts '((recents   . "r")
                                   (bookmarks . "m")
                                   (projects  . "p")
                                   (agenda    . "a")
                                   (registers . "e")))

  ;; Customize which items are displayed
  (setq dashboard-items '((recents   . 16)
                          (projects  . 8)
                          (bookmarks . 8)))

  ;; Customize which widgets to display
  (setq dashboard-startupify-list '(dashboard-insert-banner
                                    dashboard-insert-newline
                                    dashboard-insert-banner-title
                                    dashboard-insert-newline
                                    dashboard-insert-init-info
                                    dashboard-insert-items
                                    dashboard-insert-newline
                                    dashboard-insert-footer))
  :config
  (dashboard-setup-startup-hook))

;; From: emacs-china.org/t/tab-bar/26008?u=harl
(use-package tab-bar
  :ensure nil
  :hook (window-setup . tab-bar-mode)
  :init
  (setq tab-bar-separator "")
  (setq tab-bar-tab-name-function
        (lambda () (let* ((raw-tab-name (buffer-name (window-buffer (minibuffer-selected-window))))
                          (count (length (window-list-1 nil 'nomini)))
                          (truncated-tab-name (if (< (length raw-tab-name)
                                                     tab-bar-tab-name-truncated-max)
                                                  raw-tab-name
                                                (truncate-string-to-width raw-tab-name
                                                                          tab-bar-tab-name-truncated-max
                                                                          nil nil tab-bar-tab-name-ellipsis))))
                     (if (> count 1)
                         (concat truncated-tab-name "(" (number-to-string count) ")")
                       truncated-tab-name))))
  :custom
  (tab-bar-select-tab-modifiers '(control)) ; 使用 control-1 control-2 ... 来切换 tab
  (tab-bar-new-tab-choice "*dashboard*")    ; 新建 tab 打开的 buffer
  (tab-bar-tab-name-truncated-max 20)       ; tab-bar-name 最大宽度
  (tab-bar-close-button-show t)             ; 显示关闭按钮
  (tab-bar-new-button-show t)               ; 显示新增按钮
  (tab-bar-tab-hints t))                    ; 显示 tab 编号

;; mode-line 美化
(use-package doom-modeline
  :ensure t
  :hook (elpaca-after-init .  doom-modeline-mode)
  :custom
  (doom-modeline-position-column-line-format '("L%l:C%c")))

(provide 'init-ui)
;;; init-ui.el ends here
